<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="java.awt.*"
	import="java.awt.image.BufferedImage" import="javax.imageio.ImageIO"%>

<%
	response.setHeader("Cache-Control", "no-cache"); //在内存中创建图像
	int width = 60, height = 20;
	BufferedImage image = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_RGB);
	//获取画笔
	Graphics g = image.getGraphics();
	//设定背景色
	g.setColor(new Color(200, 200, 200));
	g.fillRect(0, 0, width, height);
	Random rd = new Random();
	int randNum = rd.nextInt(8999) + 1000;
	String randStr = String.valueOf(randNum);
	//将验证码存入session中
	session.setAttribute("randStr", randStr);
	//将验证码显示在图像中
	g.setColor(Color.black);
	g.setFont(new Font("", Font.PLAIN, 20));
	g.drawString(randStr, 10, 17);
	//随机产生50个干扰点
	for (int i = 0; i < 50; i++) {
		int x = rd.nextInt(width);
		int y = rd.nextInt(height);
		g.drawOval(x, y, 1, 1);
	}
	//输出图像到页面
	ImageIO.write(image, "JPEG", response.getOutputStream());
	out.clear();
	out = pageContext.pushBody();
%>